package org.galaxyworld.flycode.gui;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.galaxyworld.flycode.config.JdbcConfigData;
import org.galaxyworld.flycode.events.MainWindowEvent;
import org.galaxyworld.flycode.events.MainWindowEventListener;
import org.galaxyworld.flycode.config.ConfigManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Arrays;

public class JdbcConfigPane extends AbstractConfigPane implements MainWindowEventListener {

    private JTextField driverField;
    private JTextField urlField;
    private JTextField userNameField;
    private JTextField passwordField;
    private JTextField prefixField;

    private boolean shown = false;

    public JdbcConfigPane() {
        super("JDBC");

        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                shown = true;
            }

            @Override
            public void componentHidden(ComponentEvent e) {
                if (shown) {
                    JdbcConfigData jdbcConfigData = ConfigManager.getConfig(JdbcConfigData.class);
                    jdbcConfigData.setDriver(driverField.getText());
                    jdbcConfigData.setUrl(urlField.getText());
                    jdbcConfigData.setUserName(userNameField.getText());
                    jdbcConfigData.setPassword(passwordField.getText());
                    String[] prefixes = StringUtils.split(prefixField.getText(), ',');
                    if (ArrayUtils.isEmpty(prefixes)) {
                        prefixes = new String[] { prefixField.getText() };
                    }
                    jdbcConfigData.setPrefixList(Arrays.asList(prefixes));
                }
            }
        });
    }

    @Override
    protected void setupMainPane(JPanel mainPane) {
        GroupLayout layout = new GroupLayout(mainPane);
        mainPane.setLayout(layout);

        JLabel driverLabel = new JLabel("Driver");
        driverField = new JTextField();
        JLabel urlLabel = new JLabel("URL");
        urlField = new JTextField();
        JLabel userNameLabel = new JLabel("User Name");
        userNameField = new JTextField();
        JLabel passwordLabel = new JLabel("Password");
        passwordField = new JTextField();
        JLabel prefixLabel = new JLabel("Prefix");
        prefixField = new JTextField();
        JLabel prefixCommentLabel = new JLabel("Separate with comma (,)");
        prefixCommentLabel.setForeground(Color.LIGHT_GRAY);

        layout.setHorizontalGroup(
                layout.createSequentialGroup()
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(driverLabel)
                                .addComponent(urlLabel)
                                .addComponent(userNameLabel)
                                .addComponent(passwordLabel)
                                .addComponent(prefixLabel))
                        .addGap(MARGIN)
                        .addGroup(layout.createParallelGroup()
                                .addComponent(driverField)
                                .addComponent(urlField)
                                .addComponent(userNameField)
                                .addComponent(passwordField)
                                .addComponent(prefixField)
                                .addComponent(prefixCommentLabel))
                        .addGap(GAP));
        layout.setVerticalGroup(
                layout.createSequentialGroup()
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(driverLabel)
                                .addComponent(driverField))
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(urlLabel)
                                .addComponent(urlField))
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(userNameLabel)
                                .addComponent(userNameField))
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(passwordLabel)
                                .addComponent(passwordField))
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(prefixLabel)
                                .addComponent(prefixField))
                        .addComponent(prefixCommentLabel)
                        .addGap(GAP));
    }

    @Override
    public void windowOpened(MainWindowEvent e) {
        JdbcConfigData jdbcConfigData = ConfigManager.getConfig(JdbcConfigData.class);
        driverField.setText(jdbcConfigData.getDriver());
        urlField.setText(jdbcConfigData.getUrl());
        userNameField.setText(jdbcConfigData.getUserName());
        passwordField.setText(jdbcConfigData.getPassword());
        prefixField.setText(StringUtils.join(jdbcConfigData.getPrefixList(), ","));
    }

    @Override
    public void windowClosing(MainWindowEvent e) {
        System.out.println("save");
    }

}
